﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Runtime.Serialization;
using nRoute.Components;
using nRoute.Components.Disposer;

namespace nRoute.Samples.OfficerXcel.Model
{
    [DataContract]
    public class Worksheet 
        : INotifyPropertyChanged
    {

#region Declarations

        private const string UNTITLED_TITLE = "Untitled";
        private const int A_CHAR_VALUE = 65;

        private string _title;
        private string _author;
        private DateTime _createdOn;
        private DateTime? _updatedOn;

        private bool _isDirty;
        private Disposer _disposer;

#endregion

        public Worksheet()
        {
            // create the rows
            Rows = new List<WorksheetRow>();
            for (var _i = 1; _i <= 128; _i++)
            {
                Rows.Add(new WorksheetRow());
            }

            // create the columns
            Columns = new List<WorksheetColumn>();
            for (var _i = 0; _i < 26; _i++)
            {
                Columns.Add(new WorksheetColumn() { 
                    Title = Convert.ToChar(_i + A_CHAR_VALUE).ToString(),
                    Width = 40 
                });
            }

            // default values
            Title = UNTITLED_TITLE;
            CreatedOn = DateTime.Now;
        }

#region Properties

        [DataMember]
        public List<WorksheetRow> Rows { get; set; }

        [DataMember]
        public List<WorksheetColumn> Columns { get; set; }

        [DataMember]
        public string Title
        {
            get { return _title; }
            set
            {
                if (!string.Equals(_title, value, StringComparison.InvariantCulture))
                {
                    _title = value;
                    PropertyChanged.Notify(() => Title);
                }
            }
        }

        [DataMember]
        public string Author
        {
            get { return _author; }
            set
            {
                if (!string.Equals(_author, value, StringComparison.InvariantCulture))
                {
                    _author = value;
                    PropertyChanged.Notify(() => Author);
                }
            }
        }

        [DataMember]
        public DateTime CreatedOn
        {
            get { return _createdOn; }
            set
            {
                if (_createdOn != value)
                {
                    _createdOn = value;
                    PropertyChanged.Notify(() => CreatedOn);
                }
            }
        }

        [DataMember]
        public DateTime? UpdatedOn
        {
            get { return _updatedOn; }
            set
            {
                if (_updatedOn != value)
                {
                    _updatedOn = value;
                    PropertyChanged.Notify(() => UpdatedOn);
                }
            }
        }

        [IgnoreDataMember]
        public bool IsDirty
        {
            get { return _isDirty; }
            set
            {
                if (!_isDirty == value)
                {
                    _isDirty = value;
                    PropertyChanged.Notify(()=> IsDirty);
                }
            }
        }

        [IgnoreDataMember]
        public Disposer Disposer
        {
            get
            {
                if (_disposer == null) _disposer = new Disposer();
                return _disposer;
            }
        }

#endregion

#region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

#endregion

    }
}
